package uutalk.netcloud.talk.base.util;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;

import java.io.File;


/**
 Created by hujie on 14/12/17.
 */
public class PhotoUtil {


    public static final int REQUESTCODE_CHOICE_PHOTO = 10001; //相册
    public static final int REQUESTCODE_TAKE_PHOTO = 10002; //照相机

    /**
     选择图片
     @param mActivity
     */
    public static void choicePhoto(Activity mActivity) {
        Intent intent;
        if (Build.VERSION.SDK_INT < 19) {
            intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
        } else {
            intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media
                    .EXTERNAL_CONTENT_URI);
            intent.setType("image/*");

        }
        mActivity.startActivityForResult(intent, REQUESTCODE_CHOICE_PHOTO);
    }


    /**
     选择图片
     @param fragment
     */
    public static void choicePhoto(Fragment fragment) {
        Intent intent;
        if (Build.VERSION.SDK_INT < 19) {
            intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
        } else {
            intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media
                    .EXTERNAL_CONTENT_URI);
            intent.setType("image/*");

        }
        fragment.startActivityForResult(intent, REQUESTCODE_CHOICE_PHOTO);
    }


    private static String getImageCachePath(Context context) {
        return String.format("%s/photo", context.getExternalCacheDir().getAbsolutePath());
    }

    /**
     拍照并返回临时图片地址

     @param mActivity
     */
    public static String takePhoto(Activity mActivity) {


        String tempPhotoPath = null;
        Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File dir = new File(getImageCachePath(mActivity));
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, "ebook_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
        tempPhotoPath = file.getPath();
        Uri imageUri = Uri.fromFile(file);
        openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        mActivity.startActivityForResult(openCameraIntent, REQUESTCODE_TAKE_PHOTO);
        return tempPhotoPath;

    }

    /**
     拍照并返回临时图片地址

     @param fragment
     */
    public static String takePhoto(Fragment fragment) {

        String tempPhotoPath = null;
        Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File dir = new File(getImageCachePath(fragment.getActivity()));
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, "uutalk_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
        tempPhotoPath = file.getPath();
        Uri imageUri = Uri.fromFile(file);
        openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        fragment.startActivityForResult(openCameraIntent, REQUESTCODE_TAKE_PHOTO);
        return tempPhotoPath;

    }


    public interface OnTakePhotoListener {
        void onTakePhoto(String path);
    }

    /**
     * 显示相册还是拍照对话框
     */
    public static void showChooicePhotoDialog(final Activity context, final OnTakePhotoListener takePhotoListener) {
        String[] ss = {"相册", "拍照"};
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
        builder.setItems(ss, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    PhotoUtil.choicePhoto(context);
                }
                if (which == 1) {
                    takePhotoListener.onTakePhoto(PhotoUtil.takePhoto(context));
                }
            }
        });
        builder.show();
    }





}
